#导入游戏模块
import pygame
from pygame.locals import *
import time,random


class character:
    """玩家飞机"""
    def __init__(self,screen_temp):
        self.x = 200
        self.y = 400
        self.screen = screen_temp
        self.image = pygame.image.load("E:\images/images/me.png")
        self.bullet_list = []     #玩家子弹列表
    def display(self):
        """绘制子弹"""
        for b in self.bullet_list:
            b.display()
            if b.move():
                self.bullet_list.remove(b)
            
        """绘制玩家飞机"""
        self.screen.blit(self.image,(self.x,self.y))

    def move_left(self):
        """右移动飞机"""
        self.x -= 6
        if self.x <= 0:
            self.x=0
    def move_right(self):
        """左移动飞机"""
        self.x += 6
        if self.x >= 406:
            self.x=406
    """
    def move_upward(self):
        self.y -= 5
        if self.y <= 0:
            self.y = 0
    def move_down(self):
        self.y -= 5
        if self.y -=568
            self.y=568
    """
    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))
        print(len(self.bullet_list))#打印子弹的长度
class Bullet:
    """#定义飞机子弹类"""
    def __init__(self,screen_temp,x,y):
        self.x = x+53
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load("E:\images/images/epd.png")
    def display(self):
        """绘制子弹"""
        self.screen.blit(self.image,(self.x,self.y))
    def move(self):
        self.y -=10
        if self.y <=-20:
            return Ture

class EnemyPlane:
    """#定义敌机类"""
    def __init__(self,screen_temp):
        self.x = random.choice(range(409))
        self.y = -75
        self.screen = screen_temp
        self.image = pygame.image.load("E:\images/images/e2.png")
    def display(self):
        """绘制敌机"""
        self.screen.blit(self.image,(self.x,self.y))
    def move(self):
        self.y += 4
        
def key_control(hero_temp):
    '''键盘控制函数'''
    #执行程序的退出操作(防程序卡死)
    for event in pygame.event.get():
        if event.type == QUIT:
            print("exit()")
            exit()
    
    #获取按键信息
    pressed_keys = pygame.key.get_pressed()
    #print(pressed_keys) 
    #做判断，并执行对象的操作
    if pressed_keys[K_LEFT] or pressed_keys[K_a]:
        print("Left...")
        hero_temp.move_left()
    elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
        print("Right...")
        hero_temp.move_right()
    """    
    elif pressed_keys[K_UPWARD] or pressed_keys[K_w]:
        print("Upward")
        hero_temp.move_upward()
    elif pressed_keys[K_DOWN] or pressed_keys[K_s]:
        print("Down")
        hero_temp.move_down()
    """
    if pressed_keys[K_SPACE]:
        print("space...")
        hero_temp.fire()

        
def main():
    '''定义主程序函数'''
    #创建游戏窗口，
    screen = pygame.display.set_mode((512,568),0,0)

    #创建一个游戏背景
    background = pygame.image.load("E:\images/images/bg2.jpg")
    #英雄实例化
    hero =  character(screen)
    m = -968
    enemylist = [] #存放敌机列表
    while True:
        #绘制画面
        screen.blit(background,(0,m))
        m+=2
        if m>=-200:
            m = -968

        #绘制玩家飞机
        hero.display()

        #随机绘制敌机
        if random.choice(range(50)) == 10:
            enemylist.append(EnemyPlane(screen))
        for em in enemylist:
            em.display()
            em.move()
        #更新显示
        pygame.display.update()
        #定时显示
        time.sleep(0.04)
        #执行键盘控制
        key_control(hero)



    
#判断当前是否是主运行程序，并调用
if __name__ == "__main__":
    main()
